﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace FileMerger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lblStatus.Text = "就绪";
        }

        private string SelectTextFile()
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";
                openFileDialog.Title = "选择文本文件";
                return openFileDialog.ShowDialog() == DialogResult.OK
                    ? openFileDialog.FileName
                    : null;
            }
        }

        private void btnMerge_Click_1(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFile1.Text) || string.IsNullOrEmpty(txtFile2.Text))
            {
                MessageBox.Show("请先选择两个文件！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                string content1 = File.ReadAllText(txtFile1.Text);
                string content2 = File.ReadAllText(txtFile2.Text);
                string mergedContent = content1 + Environment.NewLine + content2;

                string outputDir = Path.Combine(Application.StartupPath, "Data");
                Directory.CreateDirectory(outputDir);   // 自动创建目录
                string outputPath = Path.Combine(outputDir, $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt");

                File.WriteAllText(outputPath, mergedContent);
                lblStatus.Text = $"文件合并成功！保存路径：{outputPath}";
            }
            catch (FileNotFoundException ex)  //问了一下常见的错误
            {
                MessageBox.Show($"文件未找到：{ex.FileName}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException ex)
            {
                MessageBox.Show($"文件读写错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("没有文件访问权限！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生未知错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSelectFile1_Click_1(object sender, EventArgs e)
        {
            if (SelectTextFile() is string path && !string.IsNullOrEmpty(path)) //路径不空
            {
                txtFile1.Text = path;
            }
        }

        private void btnSelectFile2_Click_1(object sender, EventArgs e)
        {
            if (SelectTextFile() is string path && !string.IsNullOrEmpty(path))
            {
                txtFile2.Text = path;
            }
        }
    }
}